﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using Emlyn.AudioToVideo.FileUploadManager;
using System.Collections.Generic;

namespace Emlyn.AudioToVideo.FileUploaderService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://emlynoregan.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FileUploader : System.Web.Services.WebService
    {
        [WebMethod]
        public string Create(string aUserID)
        {
            FileToUpload lfileToUpload = new FileToUpload(aUserID);

            return lfileToUpload.FileToUploadID;
        }


    
        [WebMethod]
        public void AcceptFragment(string aUserID, string aFileToUpload, UInt32 aFragmentNumber, byte[] aFragment)
        {
            if (FileToUpload.Exists(aUserID, aFileToUpload))
            {
                FileToUpload lfileToUpload = new FileToUpload(aUserID, aFileToUpload);

                lfileToUpload.AcceptFragment(aFragmentNumber, aFragment);
            }
            else
                throw new Exception("No such FileToUpload");
        }

        [WebMethod]
        public void AcceptFragmentString(string aUserID, string aFileToUpload, UInt32 aFragmentNumber, string aFragmentByteString)
        {
            byte[] lfragment = ConvertStringToByteArray(aFragmentByteString);

            AcceptFragment(aUserID, aFileToUpload, aFragmentNumber, lfragment);
        }

        private byte[] ConvertStringToByteArray(string aFragmentByteString)
        {
            List<byte> retval = new List<byte>();

            if (!String.IsNullOrEmpty(aFragmentByteString))
            {
                string[] lfragmentElemStringArray = aFragmentByteString.Split(' ');

                foreach (string lfragmentElemString in lfragmentElemStringArray)
                {
                    byte lbyteVal;

                    if (Byte.TryParse(lfragmentElemString.Trim(), out lbyteVal))
                    {
                        retval.Add(lbyteVal);
                    }
                }
            }

            return retval.ToArray();
        }

        [WebMethod]
        public void CompleteFile(string aUserID, string aFileToUpload, UInt32 aTotalNumberOfFragments, byte aChecksum)
        {
            if (FileToUpload.Exists(aUserID, aFileToUpload))
            {
                FileToUpload lfileToUpload = new FileToUpload(aUserID, aFileToUpload);

                lfileToUpload.CompleteFile(aTotalNumberOfFragments, aChecksum);
            }
            else
                throw new Exception("No such FileToUpload");
        }

        [WebMethod]
        public void Cancel(string aUserID, string aFileToUpload)
        {
            if (FileToUpload.Exists(aUserID, aFileToUpload))
            {
                FileToUpload lfileToUpload = new FileToUpload(aUserID, aFileToUpload);

                lfileToUpload.Cancel();
            }
            else
                throw new Exception("No such FileToUpload");
        }

    }
}
